﻿
namespace TFSChangeLog.Operations.Command
{
    using System;
    using System.Diagnostics;
    using System.Windows.Input;

    /// <summary>
    /// A command whose sole purpose is to 
    /// relay its functionality to other
    /// objects by invoking delegates. The
    /// default return value for the CanExecute
    /// method is 'true'.
    /// </summary>
    public class RelayCommand : ICommand
    {
        #region Fields

        /// <summary>Summary description for class level field of type <c>System.Action{object}</c>.</summary>
        private readonly Action<object> _execute;

        /// <summary>Summary description for class level field of type <c>System.Predicate{object}</c>.</summary>
        private readonly Predicate<object> _canExecute;

        #endregion // Fields

        #region Constructors

        /// <summary>Initializes a new instance of the <see cref="RelayCommand"/> class.
        /// Creates a new command that can always execute.</summary>
        /// <param name="execute">The execution logic.</param>
        public RelayCommand(Action<object> execute)
            : this(execute, null)
        {
        }

        /// <summary>Initializes a new instance of the <see cref="RelayCommand"/> class.
        /// Creates a new command.</summary>
        /// <param name="execute">The execution logic.</param>
        /// <param name="canExecute">The execution status logic.</param>
        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
                {
                throw new ArgumentNullException("execute");
                }

            this._execute = execute;
            this._canExecute = canExecute;
        }

        #endregion // Constructors

        #region ICommand Members

        /// <summary>Summary description for <c>RelayCommand</c> method.</summary>
        /// <param name="parameter">TODO: Summary description for <c>parameter</c> parameter of type <c>object</c>.</param>
        /// <returns>TODO: returns an object of type <c>bool</c>.</returns>
        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return this._canExecute == null ? true : this._canExecute(parameter);
        }

        /// <summary>Summary description for <c>CanExecuteChanged</c> event.</summary>
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        /// <summary>Summary description for <c>RelayCommand</c> method.</summary>
        /// <param name="parameter">TODO: Summary description for <c>parameter</c> parameter of type <c>object</c>.</param>
        public void Execute(object parameter)
        {
            this._execute(parameter);
        }

        #endregion // ICommand Members
    }
}